<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区 -->
    <el-card>
      <!-- 顶部搜索区 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <div class="grid-content bg-purple">
            <el-input
              @clear="getlist"
              clearable
              v-model="queryInfo.query"
              placeholder="请输入内容"
              class="input-with-select"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="querylist"
              ></el-button>
            </el-input>
          </div>
        </el-col>
        <el-col :span="4">
          <div class="grid-content bg-purple">
            <el-button type="primary" @click="$router.push('/goods/addgoods')"
              >添加商品
            </el-button>
          </div>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="list" style="width: 100%" stripe border>
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="120">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="120">
        </el-table-column>
        <el-table-column label="创建时间" width="150">
          <template v-slot="{ row }">
            {{ row.add_time | date('yyyy-MM-dd hh:mm:ss') }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200"
          >>
          <template v-slot="{ row }">
            <!-- {{ row.goods_id }} -->
            <el-button
              size="mini"
              @click="delok(row.goods_id)"
              type="primary"
              icon="el-icon-delete"
              >删除</el-button
            >

            <el-button
              size="mini"
              type="danger"
              icon="el-icon-edit"
              @click="editbut(row)"
              >修改</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 15, 30, 50]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: '',
  data() {
    return {
      //定义好的参数用于制作分页和模糊查询
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      //要渲染的列表数据
      list: [],
      // 总条数
      total: 0
    }
  },
  created() {
    this.getlist()
  },
  // mounted () {},
  computed: {},
  watch: {},
  methods: {
    //获取后天数据渲染表格
    async getlist() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取用户列表失败！')
      } //赋值data中定义的数据
      this.list = res.data.goods
      //总条数
      this.total = res.data.total
      // console.log(this.list, this.total);
    },
    //删除商品
    async delok(id) {
      const confirmResult = await this.$confirm(
        '此操作将永久删除该商品, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err) // 如果用户确认删除，则返回值为字符串 confirm // 如果用户取消了删除，则返回值为字符串 cancel // console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$http.delete(`goods/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('删除用户失败！')
      }
      this.$message.success('删除用户成功！')
      // 调用获取数据渲染页面
      this.getlist()
    },
    // 定义页码参数
    handleSizeChange(val) {
      this.queryInfo.pagesize = val
      this.getlist()
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.getlist()

      // console.log(`当前页: ${val}`);
    },
    //点击查询商品
    querylist() {
      this.queryInfo.pagenum = 1
      this.getlist()
    },
    editbut(row) {
      return this.$message.success('sorry！不允许修改')
    }
  }
}
</script>

<style lang="less" scoped></style>
